package com.zzyl.nursing.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.service.IReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 预约信息Service业务层处理
 * 
 * @author zjx
 * @date 2025-05-25
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;
    @Autowired
    private IFamilyMemberService familyMemberService;


    /**
     * 查询预约信息列表
     *
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
        return save(reservation) ? 1 : 0;
    }

    /**
     *
     * 查询当天取消预约数量
     * @param
     * @return 结果
     */
    @Override
    public Integer getCount() {
        Long userId = UserThreadLocal.getUserId();
        FamilyMember one = familyMemberService.lambdaQuery().eq(FamilyMember::getId, userId).one();
        String moblie = one.getPhone();

        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);

        Long count = this.lambdaQuery().eq(Reservation::getMobile, moblie)
                .eq(Reservation::getStatus, 2)
                .between(Reservation::getCreateTime, startTime, endTime)
                .count();
        return Integer.valueOf(String.valueOf(count));
    }
}
